
#include "xapp_config.h"

#include "pbm.h"
#include "app_util.h"

#include "xapp_access.h"
#include "xapp_ble.h"

#if USER_CHECK_ENABLED( FATIGUE )
#include "xapp_fatigue.h"
#endif 

#include "xapp_profile.h"
#include "xapp_osllv.h"
#include "em_backlight.h"

#include "userconfig/project_configs/fw.h"

#include "log.h"
#define LOG     LOG_INFO 

uint32_t pi_get_firmware_version(void)
{
    return fw_get()->fw_version; 
}

#if 1 // configuration 
////
pbd_err_code_t pi_conf_get_dev_information(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(fw_info_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    memcpy( pdata, (uint8_t*)&fw_get()->app_fw_id, sizeof(fw_info_t) - 4); 
    *p_size = sizeof(fw_info_t) - 4; 

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_dev_firmware_version(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    uint32_encode( FW_INFO_FW_VER, pdata );
    *p_size = sizeof(uint32_t);

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_dev_project_code(uint8_t *pdata, uint16_t *p_size)
{
    if( *p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    uint32_encode(FW_INFO_FW_ID, pdata);
    *p_size = sizeof(uint32_t);

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_dev_mac(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 6){
        return PBD_ERR_RESOURCE_INSUFFICIENT; 
    }

//    em_ble_get_mac( pdata );
    memcpy( pdata, em_ble_desc.localMAC, 6 );
    *p_size = 6; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_dev_id(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 16){
        return PBD_ERR_RESOURCE_INSUFFICIENT; 
    }

    *p_size = 16; 

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_mtu(uint8_t *pdata, uint16_t *p_size)
{
    if( *p_size < 2){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    uint16_t mtu = em_ble_desc.finalMTU; 
    pdata[0] = (uint8_t)(mtu >> 0);
    pdata[1] = (uint8_t)(mtu >> 8); 

    *p_size = 2; 
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_serial_no(uint8_t* pdata, uint16_t *p_size)
{
    if( *p_size < 16 )
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    *p_size = profile_nvm_read_serial_no( pdata );

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_logo(uint8_t* pdata, uint16_t *p_size)
{
    pdata[0] = profile_nvm_read_logo( );
    *p_size = 1 ;
    
    return PBD_SUCCESS; 
}
////
pbd_err_code_t pi_conf_get_user_gender(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 1){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_base_info_t* p_base_info = xapp_profile_get_base_info();
    
    pdata[0]    = p_base_info->gender;
    *p_size       = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_user_age(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 1){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_base_info_t* p_base_info = xapp_profile_get_base_info();
    pdata[0]    = p_base_info->age;
    *p_size       = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_user_height(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 1){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_base_info_t* p_base_info = xapp_profile_get_base_info();
    pdata[0]    = p_base_info->height;
    *p_size       = 1; 

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_user_weight(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 1){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_base_info_t* p_base_info = xapp_profile_get_base_info();
    pdata[0]    = p_base_info->weight;
    *p_size       = 1; 

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_user_skin(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 1)
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_base_info_t* p_base_info = xapp_profile_get_base_info();
    pdata[0] = p_base_info->skin;
    *p_size = 1;

    return PBD_SUCCESS;
}

////
pbd_err_code_t pi_conf_get_fatigue_reminder(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 6){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    #if USER_CHECK_ENABLED( FATIGUE )
    const profile_fatigue_t* fatigue = xapp_profile_get_fatigue();
    
    pdata[0]    = fatigue->control;
    pdata[1]    = fatigue->interval;
    pdata[2]    = fatigue->start.hour;
    pdata[3]    = fatigue->start.minute;
    pdata[4]    = fatigue->stop.hour;
    pdata[5]    = fatigue->stop.minute;
    
    *p_size       = 6; 

    return PBD_SUCCESS; 
    #else
    *p_size = 6;
    return PBD_SUCCESS;
    #endif 
}
pbd_err_code_t pi_conf_get_do_not_disturb(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < 5){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_DND_t* DND = xapp_profile_get_DND();
    pdata[0]    = DND->enabled;
    pdata[1]    = DND->start.hour;
    pdata[2]    = DND->start.minute;
    pdata[3]    = DND->stop.hour;
    pdata[4]    = DND->stop.minute;    
    *p_size       = 5; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_target_of_day(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_target_step_t* step_of_target = xapp_profile_get_target();

    uint32_encode( step_of_target->day, pdata);
    *p_size       = 4; 
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_get_target_of_week(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_target_step_t* step_of_target = xapp_profile_get_target();
    uint32_encode( step_of_target->week, pdata);
    *p_size       = 4; 
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_proximity(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_proximity_t* prox = xapp_profile_get_proximity();
    pdata[0]        = prox->enabled;
    pdata[1]        = prox->condition;
    pdata[2]        = prox->action;
    pdata[3]        = prox->time;
    *p_size           = 4; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_notification_category(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_notification_t* p_notif = xapp_profile_get_notification();
    uint32_encode( p_notif->category, pdata);
    *p_size           = 4; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_notification_application(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint32_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_notification_t* p_notif = xapp_profile_get_notification();
    uint32_encode( p_notif->application, pdata);
    *p_size           = 4; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_heartrate_setting(uint8_t *pdata, uint16_t *psize)
{
    if(*psize < 4)
        return PBD_ERR_RESOURCE_INSUFFICIENT;

    const profile_heartrate_t* hr = xapp_profile_get_heartrate();
    uint16_encode( hr->interval, pdata);
    pdata[2] = hr->high_threshold;
    pdata[3] = hr->low_threshold;
    *psize = 4; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_bloodpressure_setting(uint8_t *pdata, uint16_t *psize)
{
    if( *psize < 6 )
    return PBD_ERR_RESOURCE_INSUFFICIENT;

    const profile_bloodpressure_t* bp = xapp_profile_get_bloodpressure();
    uint16_encode( bp->interval, pdata); 
    pdata[2] = bp->sbp.high_threshold;
    pdata[3] = bp->sbp.low_threshold;
    pdata[4] = bp->dbp.high_threshold;
    pdata[5] = bp->dbp.low_threshold;
    *psize = 6; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_temperature_setting(uint8_t *pdata, uint16_t *psize)
{
    if( *psize < 6)
    return PBD_ERR_RESOURCE_INSUFFICIENT;

    const profile_temperature_t* temp = xapp_profile_get_temperature();
    uint16_encode( temp->interval, pdata);
    uint16_encode( temp->high_threshold, pdata + 2); 
    uint16_encode( temp->low_threshold, pdata + 4); 
    *psize = 6; 

    return PBD_SUCCESS;
}

pbd_err_code_t pi_conf_get_sleep_option(uint8_t* pdata, uint16_t *psize)
{
    if( *psize < 5 )
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_sleep_t* sleep = xapp_profile_get_SLEEP();
    pdata[0] = sleep->enabled;
    pdata[1] = sleep->start.hour;
    pdata[2] = sleep->start.minute;
    pdata[3] = sleep->stop.hour;
    pdata[4] = sleep->stop.minute;
    *psize = 5; 

    return PBD_SUCCESS; 
}
////
pbd_err_code_t pi_conf_get_system_language(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }
    
    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->langauage;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_time_format(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }
    
    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->time_format;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_unit(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }    

    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->unit;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_smart_light_on(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }
    
    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->smart_light_on;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_time_of_light_on(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }
    
    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->time_of_light_on;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_brightness(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }    

    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->brightness;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_wearing_habit(uint8_t *pdata, uint16_t *p_size)
{
    if(*p_size < sizeof(uint8_t)){
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }
    
    const profile_system_t* system = xapp_profile_get_system();
    pdata[0]        = system->wearing_habit;
    *p_size           = 1; 

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_watchface(uint8_t *pdata, uint16_t *p_size) 
{
    if( *p_size < 1)
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_system_t* system = xapp_profile_get_system();
    pdata[0] = system->watchface;
    *p_size = 1;

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_get_system_view_style(uint8_t* pdata, uint16_t *p_size)
{
    if( *p_size < 1 )
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_system_t* system = xapp_profile_get_system();
    pdata[0] = system->view_style;
    *p_size = 1;

    return PBD_SUCCESS;
}

pbd_err_code_t pi_conf_get_system_motor_level(uint8_t* pdata, uint16_t *p_size)
{
    if( *p_size < 1 )
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_system_t* system = xapp_profile_get_system();
    pdata[0] = system->motor_vibrate_level;

    *p_size = 1;

    return PBD_SUCCESS; 
}

pbd_err_code_t pi_conf_get_bt_setting(uint8_t* pdata, uint16_t *p_size)
{
    if( *p_size < 2 ) 
    {
        return PBD_ERR_RESOURCE_INSUFFICIENT;
    }

    const profile_bt_t* btm = xapp_profile_get_btmodule();
    pdata[0] = btm->is_bt_power_on;
    pdata[1] = btm->is_bt_open;
    *p_size = 2;
    return PBD_SUCCESS; 
}


pbd_err_code_t pi_conf_write_serial_no(uint8_t *pdata, uint16_t size)
{
    if( profile_nvm_write_serial_no( pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_LENGTH; 
    }

    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_write_logo(uint8_t *pdata, uint16_t size)
{
    if( profile_nvm_write_logo( pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_LENGTH; 
    }

    return PBD_SUCCESS; 
}

pbd_err_code_t pi_conf_write_user_gender(uint8_t *pdata, uint16_t size)
{
    if( size != 1 )
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    if( xapp_profile_write_conf( PROFILE_TAG_GENDER, pdata, size ) > 0 ){
        return PBD_ERR_INVALID_FORMAT;
    }    

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_user_age(uint8_t *pdata, uint16_t size)
{
    if( size != 1) 
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    if( xapp_profile_write_conf( PROFILE_TAG_AGE, pdata, size )  > 0)
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_user_height(uint8_t *pdata, uint16_t size)
{
    if( size != 1) 
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    if( xapp_profile_write_conf( PROFILE_TAG_HEIGHT, pdata, size )  > 0)
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_PERSON_height_update();

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_user_weight(uint8_t *pdata, uint16_t size)
{
    if( size != 1) {
        return PBD_ERR_INVALID_LENGTH;
    }
    if( xapp_profile_write_conf( PROFILE_TAG_WEIGHT, pdata, size ) > 0){
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_PERSON_weight_update();

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_user_skin(uint8_t *pdata, uint16_t size)
{
    if( size != 1)
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    if( xapp_profile_write_conf( PROFILE_TAG_SKIN, pdata, size ) > 0)
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_PERSON_skin_update();

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_fatigue_reminder(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_FATIGUE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    #if USER_CHECK_ENABLED( FATIGUE )
    xapp_fatigue_load_from_profile();
    #endif 
    

    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_do_no_disturb(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_DND, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_DND_update();
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_target_of_day(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_TARGET_OF_DAY, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    xapp_access_STEPSGOAL_update();
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_target_of_week(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_TARGET_OF_WEEK, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_write_proximity_alert(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_PROXIMITY, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_notification_category(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_NOTIF_CATEGORY, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_notification_application(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_NOTIF_APPLICATION, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_heartrate(uint8_t *pdata, uint16_t size)
{
    #if USER_CHECK_ENABLED( HRS)
    if( xapp_profile_write_conf( PROFILE_TAG_HEARTRATE, pdata, size ) > 0 )
        return PBD_ERR_INVALID_FORMAT;

    //iqw_app_access_get_heartrate_switch
    xapp_access_HEARTRATE_update();
    #endif 
    
    return PBD_SUCCESS;
     
   
}
pbd_err_code_t pi_conf_write_bloodpressure(uint8_t* pdata, uint16_t size)
{
    #if USER_CHECK_ENABLED( HRS )
    if( xapp_profile_write_conf( PROFILE_TAG_BLOODPRESSURE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_BLOODPRESSURE_update();
    #endif 
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_temperature(uint8_t* pdata, uint16_t size)
{
#ifdef FUNC_TEMP
    if( xapp_profile_write_conf( PROFILE_TAG_TEMPERATURE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    return PBD_SUCCESS; 
#else
    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
#endif 
}

pbd_err_code_t pi_conf_write_sleep_option(uint8_t* pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_SLEEP, pdata, size) > 0)
    {
        return PBD_ERR_INVALID_FORMAT;
    }

    xapp_access_SLEEP_update();
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_language(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_LANGAUAGE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    xapp_access_LANGAUAGE_update();
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_time_format(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_TIME_FORMAT, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    xapp_access_TIMEFORMAT_update();
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_unit(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_UNIT, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_smart_light_on(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_SMART_LIGHT_ON, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    xapp_access_SMART_LIGHT_ON_update();
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_time_of_light_on(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_TIME_OF_LIGHT_ON, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_brightness(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_BRIGHTNESS, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    xapp_access_BRIGHTNESS_update();
    
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_wearing_habit(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_WEARING_HABIT, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_FORMAT;
    }
    
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_conf_write_system_watchface(uint8_t *pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_WATCHFACE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_PARAMS; 
    }

    xapp_access_WATCHFACE_update();
    return PBD_SUCCESS;
}
pbd_err_code_t pi_conf_write_system_view_style(uint8_t* pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_VIEW_STYLE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_PARAMS; 
    }

    return PBD_SUCCESS;
}

pbd_err_code_t pi_conf_write_system_motor_vibrate_level(uint8_t* pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_MOTOR_VIBRATE_LEVEL, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_PARAMS;
    }

    xapp_access_MOTOR_level_udpate( pdata[0] );
    return PBD_SUCCESS; 
}

pbd_err_code_t pi_conf_write_bt_setting(uint8_t* pdata, uint16_t size)
{
    if( xapp_profile_write_conf( PROFILE_TAG_BTMODULE, pdata, size ) > 0 )
    {
        return PBD_ERR_INVALID_PARAMS; 
    }

    return PBD_SUCCESS; 
}



void pi_conf_save_profile(void)
{
    xapp_profile_store();
}
#endif 
